package com.atstudying.mall.order.service.impl;

import com.atstudying.common.utils.PageUtils;
import com.atstudying.common.utils.Query;
import com.atstudying.mall.order.dao.OrderItemDao;
import com.atstudying.mall.order.entity.OrderEntity;
import com.atstudying.mall.order.entity.OrderItemEntity;
import com.atstudying.mall.order.service.OrderItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@RabbitListener(queues="hello-java-queue")

@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    /**
     * 参数可以写以下类型
     * 1：Message message：原生消息详细信息。头+体
     * 2：T<发送消息的类型></>OrderReturnReasonEntity contents
     * 3：Channel channel：当前传输数据的通道
     *
     * Queue：可以让很多人来监听。只要收到消息，队列删除消息，而且只可以有一个收到此消息
     * 场景：
     * 1）订单服务启动多个：同一个消息，只可以有一个客户端收到
     * 2）只有一个消息完全处理完，方法运行结束，我们就可以收到下一个消息
     */
    //Object可以获取到我们接受到的消息
    @RabbitHandler
    public void receiveMessage1(OrderEntity message){
        System.out.println("111");
        System.out.println("接受到的消息。。内容:"+message+"==>类型"+message.getClass());

    }
    @RabbitHandler
    public void receiveMessage2(OrderItemEntity content, Message message, com.rabbitmq.client.Channel channel) throws IOException {
        System.out.println("222");

        //消息头属性信息
        MessageProperties messageProperties = message.getMessageProperties();

        //channel内按顺序自增
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("deliveryTag："+deliveryTag);
        System.out.println("接受到的消息。。内容:"+content+"==>类型"+content.getClass());

        //签收货物，非批量模式
        try {
            if (deliveryTag%2==0){
                channel.basicAck(deliveryTag,false);
                System.out.println("签收了货物:"+deliveryTag);
            }else {
                channel.basicNack(deliveryTag,false,false);

                System.out.println("没有签收了货物："+deliveryTag);
            }



        }catch (Exception e){

            //网络中断
        }
    }
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }


}